package org.demo.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import org.demo.service.interfaces.UserService;

@Controller
public class IndexController{
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/showUser", produces="application/json;charset=UTF-8")
	public String showUser(HttpServletRequest request,Model model){
		int userId= Integer.parseInt(request.getParameter("id"));
		JSONObject resultUser = userService.getUserByid(userId);
		model.addAttribute("user",resultUser);
		return "showUser";
	}
	
	@ResponseBody
	@RequestMapping(value="/getUser", produces="application/json;charset=UTF-8")
	public Object getUser(HttpServletRequest request){
		int userId= Integer.parseInt(request.getParameter("id"));
		JSONObject resultUser = userService.getUserByid(userId);
		return resultUser;
	}
	
	@ResponseBody
	@RequestMapping(value="/index", produces="application/json;charset=UTF-8")
	public String index(HttpServletRequest request){
		
		return "index";
	}
	
	@ResponseBody
	@RequestMapping(value="/json", produces="application/json;charset=UTF-8")
	public Object json(HttpServletRequest request){
		JSONObject resultUser = new JSONObject();
		
		return resultUser;
	}
	
}